#ifndef __DEF_WXSQLITE3_H__
#define __DEF_WXSQLITE3_H__

#include <wx/wx.h>
#include <vector>

//pre-define
class wxSQLite3Database;
class wxSQLite3ResultSet;
class wxSQLite3Statement;

class wxSQLite3Database {
public:
	wxSQLite3Database(void);
	~wxSQLite3Database(void);
	
	void Open(const wxString &filename, bool autocommit = true);
	void Open(bool autocommit = true)
	{
		Open(_T("t"),autocommit);
	}
	void Close(void);
	void Commit(void);
	void Rollback(void);
	bool IsOpened(void) { return is_opened; };
	bool IsUpdated(void) { return is_updated; };
	void SetOption(const wxString &option);
	wxSQLite3ResultSet ExecuteQuery(const wxString &sql);
	wxSQLite3ResultSet ExecuteQuery(wxSQLite3Statement &sql);
	int ExecuteUpdate(const wxString &sql);
	int ExecuteUpdate(wxSQLite3Statement &sql);
	void SetFileName(const wxString &filename)
	{
		db_filepath = filename;
	}
private:
	wxString db_command;
	wxString db_filepath;
	wxString db_option;
	
	bool is_opened;
	bool is_updated;
	bool is_autocommit;
	bool commited;
	wxString tmp_filepath;
	wxString GetCurrentPath(void)
	{
		if (is_autocommit) {
			return db_filepath;
		}else{
			return tmp_filepath;
		}
	}
	void create_TempDB(void);
	void commit_TempDB(void);
	void rollback_TempDB(void);
	void delete_TempDB(void);
};

class wxSQLite3ResultSet
{
public:
	wxSQLite3ResultSet(void) { index = -1; };
	wxSQLite3ResultSet(wxArrayString &content, wxString &option);
	~wxSQLite3ResultSet(void) {};
	
	int GetInt(int column);
	wxString GetString(int column);
	bool GetBool(int column);
	wxDateTime GetDate(int column);
	int GetCount(void) { return db_content.GetCount(); };
	bool NextRow(void);
private:
	wxArrayString db_content;
	wxString db_option;
	int index;
};
class wxSQLite3Statement
{
public:
	wxSQLite3Statement(void) {};
	~wxSQLite3Statement(void) {};
	
	void SetColumn(const wxString &str) { columnstr = str; };
	void SetTable(const wxString &str) { tablestr = str; };
	void SetWhere(const wxString &str) { wherestr = str; };
	wxString GetColumn(void) { return columnstr; };
	wxString GetTable(void) { return tablestr; };
	wxString GetWhere(void) { return wherestr; };
	wxString GetSQL(void)
	{
		if (wherestr.IsEmpty()) {
			return wxString::Format(_T("select %s from %s ;"),
				columnstr.c_str(),tablestr.c_str());
		}else{
			return wxString::Format(_T("select %s from %s where %s ;"),
				columnstr.c_str(),tablestr.c_str(),wherestr.c_str());
		}
	}
private:
	wxString columnstr;
	wxString tablestr;
	wxString wherestr;
};

#endif